<article id="wikiArticle">
<div></div>
<div> </div>
<div><strong><code>trimStart() </code></strong>方法从字符串的开头删除空格。<code>trimLeft()</code>是此方法的别名。</div>
<div> </div>
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/string-trimstart.html" width="100%"></iframe></div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><var>str</var>.trimStart();
<var>str</var>.trimLeft();</code></pre>
<h3 id="返回值">返回值</h3>
<p>一个新字符串，表示从其开头（左端）剥离空格的调用字符串。</p>
<h2 id="Description" name="Description">描述</h2>
<p><code>trimStart()</code> / <code>trimLeft()</code>方法移除原字符串左端的连续空白符并返回，<code>trimStart()</code> / <code>trimLeft()</code>方法并不会直接修改原字符串本身。</p>
<h3 id="别名">别名</h3>
<p>为了与 <a href="Reference/Global_Objects/String/padStart" title="padStart() 方法用另一个字符串填充当前字符串(重复，如果需要的话)，以便产生的字符串达到给定的长度。填充从当前字符串的开始(左侧)应用的。"><code>String.prototype.padStart</code></a> 等函数保持一致，标准方法名称为<code>trimStart</code>。 但是，出于Web兼容性原因，<code>trimLeft</code>仍然是<code>trimStart</code>的别名。在某些引擎中，这意味着：</p>
<pre><code  class="language-javascript"><code>String.prototype.trimLeft.name === "trimStart";</code>
</code></pre>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Using_toLowerCase" name="Example:_Using_toLowerCase">使用<code>trimStart()</code></h3>
<p>下面的例子输出了小写的字符串"foo  "：</p>
<pre><code class="language-js;highlight:[5]">var str = "   foo  ";

alert(str.length); // 8

str = str.trimLeft();
alert(str.length); // 5
document.write( str );
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a class="external" href="https://github.com/tc39/proposal-string-left-right-trim/#stringprototypetrimstart--stringprototypetrimend" rel="noopener">String.prototype.{trimStart,trimEnd}</a></code> proposal</td>
<td>Stage 1</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><a class="new" href="Reference/Global_Objects/String/trimStart" rel="nofollow"><code>trimStart</code></a></th><td class="bc-supports-yes bc-browser-chrome bc-has-history"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              66<div class="bc-icons"></div><section class="bc-history" id="sect1"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              66<div class="bc-icons"></div></dt><dd></dd><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes<div class="bc-icons"><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> </div></dt><dd><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> Uses the non-standard name: <code>trimLeft</code></dd></dl></section></td><td class="bc-supports-unknown bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr title="Compatibility unknown; please update this.">
                ?
              </abbr></td><td class="bc-supports-yes bc-browser-firefox bc-has-history"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              61<div class="bc-icons"></div><section class="bc-history" id="sect2"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              61<div class="bc-icons"></div></dt><dd></dd><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              3.5<div class="bc-icons"><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> </div></dt><dd><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> Uses the non-standard name: <code>trimLeft</code></dd></dl></section></td><td class="bc-supports-no bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              53</td><td class="bc-supports-unknown bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr title="Compatibility unknown; please update this.">
                ?
              </abbr></td><td class="bc-supports-yes bc-browser-webview_android bc-has-history"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              66<div class="bc-icons"></div><section class="bc-history" id="sect3"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              66<div class="bc-icons"></div></dt><dd></dd><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes<div class="bc-icons"><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> </div></dt><dd><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> Uses the non-standard name: <code>trimLeft</code></dd></dl></section></td><td class="bc-supports-yes bc-browser-chrome_android bc-has-history"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              66<div class="bc-icons"></div><section class="bc-history" id="sect4"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              66<div class="bc-icons"></div></dt><dd></dd><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes<div class="bc-icons"><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> </div></dt><dd><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> Uses the non-standard name: <code>trimLeft</code></dd></dl></section></td><td class="bc-supports-unknown bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr title="Compatibility unknown; please update this.">
                ?
              </abbr></td><td class="bc-supports-yes bc-browser-firefox_android bc-has-history"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              61<div class="bc-icons"></div><section class="bc-history" id="sect5"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              61<div class="bc-icons"></div></dt><dd></dd><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              4<div class="bc-icons"><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> </div></dt><dd><abbr class="only-icon" title="Uses the non-standard name: &lt;code&gt;trimLeft&lt;/code&gt;"><span>Alternate Name</span><i class="ic-altname"></i></abbr> Uses the non-standard name: <code>trimLeft</code></dd></dl></section></td><td class="bc-supports-yes bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              53</td><td class="bc-supports-unknown bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr title="Compatibility unknown; please update this.">
                ?
              </abbr></td><td class="bc-supports-unknown bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr title="Compatibility unknown; please update this.">
                ?
              </abbr></td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              10.0.0</td></tr></tbody></table><section class="bc-legend" id="sect6"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
<span>Full support</span>
                  
                </abbr></span></dt><dd>Full support</dd><dt><span class="bc-supports-no bc-supports">
<abbr class="bc-level bc-level-no only-icon" title="No support">
<span>No support</span>
                  
                </abbr></span></dt><dd>No support</dd><dt><span class="bc-supports-unknown bc-supports">
<abbr class="bc-level bc-level-unknown only-icon" title="Compatibility unknown">
<span>Compatibility unknown</span>
                  
                </abbr></span></dt><dd>Compatibility unknown</dd><dt><abbr class="only-icon" title="Uses a non-standard name."><span>Uses a non-standard name.</span><i class="ic-altname"></i></abbr></dt><dd>Uses a non-standard name.</dd></dl></section></div><p></p>
<h2 id="参见">参见</h2>
<ul>
<li><a href="Reference/Global_Objects/String/trim" title="trim() 方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符（如 LF，CR）。"><code>String.prototype.trim()</code></a></li>
<li><a class="new" href="Reference/Global_Objects/String/trimEnd" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>String.prototype.trimEnd()</code></a></li>
</ul>
</article>